const app = getApp()

const ctx = wx.createCanvasContext('myCanvas')
//获取应用实例  
Page({
  data: {
    pics: [],
    num: "100",
    text_x: 20, //x轴
    text_y: 20, //y轴
    imageUrl: '',  // 生成的图片路径
    showst: false, //是否完成图片和文字的填入
    mysrc1: '', //文本
    url1: 'https://apitest.cndzys.com/uploads/1533103119.jpg',
    thinkList: [],
    lineHeight: 50,
    content: '王小波的黄金时代有一片紫色的天空'
  },
  //登录检测
  dljc: function () {
    var openid = getApp().globalData.openid;
    var nickname = getApp().globalData.nickName;
    var that = this;
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_dljc',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': openid,
        'nickname': nickname,
      },
      header: {//请求头  
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        that.setData({ jifen: res.data.jifen, })
        if (res.data.error == "1") {
          that.setData({ text: 2 })
        }
      }
    })
  },
  //弹出积分详情
  jifen_open: function () {
    this.setData({
      text1: "1",
    })
  },
  //关闭积分详情
  jifen_close: function () {
    this.setData({
      text1: "2",
    })
  },

  onShareAppMessage: function () {
    return {
      title: getApp().globalData.share_words,
      path: 'pages/contents/contents?img_url=' + getApp().globalData.time_name + '&share_title=' + getApp().globalData.share_title + '&tjr_openid=' + getApp().globalData.openid,
      success: function (res) {
        // 转发成功
        console.log("转发成功");
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败");
      }
    }



  },





  onLoad: function (options) {
    // console.log("传过来的是");
    // console.log(options.people_type1);
    // console.log(options.img_url);
    getApp().globalData.img_url = getApp().globalData.API_URI + "/public/upload/weixin1/" + options.img_url;
    //getApp().globalData.people_type1 = options.people_type1;
    this.dljc();
    //分享事件
    wx.showShareMenu({
      withShareTicket: true,
    })
    //页面合成的图片
    console.log("页面合成的图片路径" + getApp().globalData.img_url);
    this.change();




    //是否播放音乐
    //this.listenerButtonPlay();
    wx.onBackgroundAudioPlay(function () {
      console.log('onBackgroundAudioPlay')
    })

		/**
		 * 监听音乐暂停
		 */
    wx.onBackgroundAudioPause(function () {
      console.log('onBackgroundAudioPause')
    })


		/**
		 * 监听音乐停止
		 */
    wx.onBackgroundAudioStop(function () {
      console.log('onBackgroundAudioStop')
    })

  },//播放音乐
  listenerButtonPlay: function () {
    console.log("音乐播放");
    this.setData({
      mp3: 1
    })
    wx.playBackgroundAudio({
      //播放地址
      dataUrl: 'https://apitest.cndzys.com/wzf/1.mp3',
      title: '青云志',
      //图片地址
      coverImgUrl: 'http://r1.ykimg.com/050E0000576B75F667BC3C136B06E4E7'

    })
  },
  /**
* 监听button暂停按钮
*/
  listenerButtonPause: function () {
    console.log("音乐暂停");
    this.setData({
      mp3: 0
    })

    wx.pauseBackgroundAudio();
  },




  fanhui: function () {
    console.log("这是点击了返回事件");
    var num = this.data.num -= 1;
    num = 100 - num;
    console.log('我点击了' + num + '下');

    var pics = this.data.pics;
    var pics_leng = this.data.pics.length;

    if (pics_leng - num <= "0") {
      console.log('没有图片了');
    }
    else {
      var img_url = pics[num - 1];
      var img_url1 = img_url.split("=====");
      var img_url = img_url1[0];
      var img_id = img_url1[1];
      var share_title = img_url1[2];
      var share_words = img_url1[3];
      console.log('img_id===' + img_id);
      console.log('img_url====' + img_url);
      this.setData({ img_url: img_url });
      getApp().globalData.time_name = img_url;
      getApp().globalData.share_title = share_title;
      getApp().globalData.share_words = share_words;      
    }
  },

  share: function () {
    console.log("这里是分享的事件");
    var openid = getApp().globalData.openid;
    var nickname = getApp().globalData.nickName;
    var that = this;
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_share',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': openid,
        'nickname': nickname,
      },
      header: {//请求头  
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        that.setData({ jifen: res.data.jifen, })
      }
    })
  },
  save: function () {
    console.log("这里是保存的事件");
    // var share_title = getApp().globalData.share_title;
    // console.log("share_title===" + share_title);
    // this.setData({ share_title: share_title })
    this.setData({ bcfx: "1" })


    var that = this;
    let i = 0;
    let lineNum = 1;
    let thinkStr = '';
    let thinkList = [];
    for (let item of getApp().globalData.share_title) {
      if (item === '\n') {
        thinkList.push(thinkStr);
        thinkList.push('a');
        i = 0;
        thinkStr = '';
        lineNum += 1;
      } else if (i === 10) {
        thinkList.push(thinkStr);
        i = 1;
        thinkStr = item;
        lineNum += 1;
      } else {
        thinkStr += item;
        i += 1;
      }
    }
    thinkList.push(thinkStr);
    that.setData({ thinkList: thinkList });
    var that = this
    wx.downloadFile({
      url: getApp().globalData.time_name,
      success: function (sres) {
        
        getApp().globalData.tempFilePath = sres.tempFilePath
        //that.data.mysrc1 = sres.tempFilePath
        console.log("这是回调下载回来的图片本地路径===" + sres.tempFilePath);
        ctx.drawImage(getApp().globalData.tempFilePath, 0, 0, 270, 370)
        ctx.globalAlpha = 1;
        ctx.fillStyle = "white";
        ctx.fillRect(0, 370, 270, 105);
        ctx.drawImage("../../images/code.jpg", 10, 375, 70, 70)
        let height = 400;
        for (let item of that.data.thinkList) {
          if (item !== 'a') {
            console.log(item);
            ctx.setFillStyle("#333");
            ctx.setFontSize(15);
            ctx.fillText(item, 90, height)
            height += that.data.lineHeight - 30;
          }
        }
        ctx.draw()
        

      },
    })
  },

  save_quxiao: function () {
    console.log("这里是保存取消的事件");
    this.setData({ bcfx: "2" })
  },
  save_baocun: function () {
    console.log("这里是保存的手机事件");
    this.setData({ bcfx: "2" })

  },
  Okgenerate() { //生成图片方法
    var _this = this
    this.setData({
      showst: false
    })
    wx.canvasToTempFilePath({ //生成图片
      x: 0,
      y: 0,
      width: 270,
      height: 480,
      destWidth: 270,
      destHeight: 480,
      quality: 1,
      fileType: 'jpg',
      canvasId: 'myCanvas',
      success: function (res) {
        wx.saveImageToPhotosAlbum({  //保存生成的图片到手机相册里
          filePath: res.tempFilePath,
          success(res) {
            app.showToasts('保存成功')
            _this.setData({
              showst: true
            })
          }
        })
      }
    })
    this.setData({ bcfx: "2" })
    var that=this;
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_baocun',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': getApp().globalData.openid,
        'nickname': getApp().globalData.nickName,
        'face_id': that.data.face_id,

      },
      header: {//请求头  
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        that.setData({ jifen: res.data.jifen, })


        that.setData({ bccg: 1 })
        setTimeout(function () {
          //wx.navigateTo({ url: '../index/index', })
          that.setData({ bccg: 2 })
        }, 2000)



      }
    })
    wx.clearStorageSync();
  },

  change: function () {
    this.setData({ num: "100", img_url11: "1" })
    var openid = getApp().globalData.openid;
    var nickname = getApp().globalData.nickName;
    var img_url = getApp().globalData.img_url;
    var people_type1 = getApp().globalData.people_type1;
    console.log("变的人是====" + getApp().globalData.people_type1);
    var that = this;
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_change',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': openid,
        'nickname': nickname,
        'people_type1': people_type1,
        'img_url': img_url,
      },
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: 'POST',
      success: function (res) {
        if (res.data.cond == "1") {
          that.setData({ error: res.data.error, })
          that.setData({ error1: 1 })
          that.setData({ img_url11: 2 })
          setTimeout(function () {
            wx.navigateTo({ url: '../index/index', })
            that.setData({ error1: 2 })
          }, 2000)
        }
        getApp().globalData.share_title = res.data.share_title;
        getApp().globalData.share_words = res.data.share_words;
        getApp().globalData.time_name = res.data.time_name;
        var pics = that.data.pics.concat(res.data.time_name + '=====' + res.data.face_id + '=====' + res.data.share_title + '=====' + res.data.share_words)
        that.setData({
          pics: pics
        })
        
        that.setData({ jifen: res.data.jifen, })
        that.setData({ face_id: res.data.face_id, })
        that.setData({ img_url: res.data.img_url, })
        that.setData({ pics: pics })
        that.setData({ img_url11: "2" })
      }

    })
  },


	/**
	* 播放状态
	*/
  listenerButtonGetPlayState: function () {
    wx.getBackgroundAudioPlayerState({
      success: function (res) {
        console.log(res)
        //duration 总时长
        //currentPosition 当前播放位置
        //status 播放状态
        //downloadPercent 下载状况 100 即为100%
        //dataUrl 当前播放音乐地址
      }
    })
  },

	/**
	* 设置进度
	*/
  listenerButtonSeek: function () {
    wx.seekBackgroundAudio({
      position: 30
    })
  },
	/**
	*停止播放 
	*/
  listenerButtonStop: function () {
    wx.stopBackgroundAudio()
  }
})
